package cn.team11.smarteye.utils;

/**
 * Created by cylan on 2015/1/19.
 */


import android.util.Log;


import java.util.Locale;
import java.util.UnknownFormatConversionException;

/**
 * Wrapper API for sending log output.
 */
public class AppLogger {
    public static boolean permissionGranted = false;
    private static final String tagLeft = "(";
    private static final String tagRight = "):";
    private static final String tagL = "L:";
    private static final StringBuilder builder = new StringBuilder();
    protected static final String TAG = "t11Log";
    //    public static boolean DEBUG = BuildConfig.DEBUG;
    public static boolean DEBUG = true;



    private AppLogger() {
    }

    public static void enableDebug(boolean debug) {
        DEBUG = debug;
    }

    /**
     * Send activity_cloud_live_mesg_call_out_item VERBOSE log message.
     *
     * @param msg The message you would like logged.
     */
    public static void v(String msg) {
        final String content = buildMessage(msg);
        if (DEBUG)
            Log.v(TAG, content);
    }


    /**
     * Send activity_cloud_live_mesg_call_out_item DEBUG log message.
     *
     * @param msg
     */
    public static void d(String msg) {
        final String content = buildMessage(msg);
        if (DEBUG)
            Log.d(TAG, buildMessage(msg));
    }


    /**
     * Send an INFO log message.
     *
     * @param msg The message you would like logged.
     */
    public static void i(String msg) {
        final String content = buildMessage(msg);
        if (DEBUG)
            Log.i(TAG, buildMessage(msg));
    }

    /**
     * Send an ERROR log message.
     *
     * @param msg The message you would like logged.
     */
    public static void e(String msg) {
        final String content = buildMessage(msg);
        if (DEBUG)
            Log.e(TAG, content);
    }

    /**
     * Send an ERROR log message.
     *
     * @param throwable The message you would like logged.
     */
    public static void e(Throwable throwable) {
        final String content = buildMessage(throwable == null ? null : throwable.getMessage());
        if (DEBUG)
            Log.e(TAG, content);
    }


    /**
     * Send activity_cloud_live_mesg_call_out_item WARN log message
     *
     * @param msg The message you would like logged.
     */
    public static void w(String msg) {
        final String content = buildMessage(msg);
        if (DEBUG)
            Log.w(TAG, buildMessage(msg));
    }

    /**
     * Send an empty WARN log message and log the exception.
     *
     * @param thr An exception to log
     */
    public static void w(Throwable thr) {
        if (DEBUG)
            Log.w(TAG, buildMessage(""), thr);
    }

   private static final String content = "%s-%s(L:%s):%s";

    /**
     * Building Message
     *
     * @param msg The message you would like logged.
     * @return Message String
     */
    protected static String buildMessage(String msg, Object... args) {
        StackTraceElement caller = new Throwable().fillInStackTrace()
                .getStackTrace()[2];
        return String.format(Locale.getDefault(), content, Thread.currentThread().toString(),
                caller.getFileName(), caller.getLineNumber(), createMessage(msg, args));
    }

    private static String createMessage(String message, Object... args) throws UnknownFormatConversionException {
        return args.length == 0 ? message : String.format(message, args);
    }
}

